//
//  MLBleControlViewController.m
//  RuZe
//
//  Created by 程杰 on 2023/5/31.
//

#import "MLBleControlViewController.h"
#import "SVProgressHUD.h"

@interface MLBleControlViewController ()
@property (weak, nonatomic) IBOutlet UITextField *sendTf;

@property (weak, nonatomic) IBOutlet UILabel *resultLb;



@property (assign, nonatomic) BOOL isBack;
@end

@implementation MLBleControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"断开" style:UIBarButtonItemStylePlain target:self action:@selector(stopConnect)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    self.resultLb.numberOfLines = 0;
    
    // 1. 先连接上外设
    [[MLBLEManager sharedManager] mlBleConnectCBPeripheralModel:self.bleModel];
    
    // 2. 保持外设一直连接，才可以进行其他操作
    [SVProgressHUD show];
    [[MLBLEManager sharedManager] mlBleCBStateLinsten:^(MLBLEConnectCBState state) {
        
        if (state == MLBLEConnectCBDisconnect) {
            
            if (self.isBack){
                return;
            }
            
            [SVProgressHUD show];
            [[MLBLEManager sharedManager] mlBleConnectCBPeripheralModel:self.bleModel];
            
        } else if (state == MLBLEConnectCBNotifyOK) {
            
            [SVProgressHUD dismiss];
        }
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.isBack = YES;
    
    [self stopConnect];
}

// 断开连接
- (void)stopConnect {
    
    [[MLBLEManager sharedManager] mlBleDisconnectPeripheral:self.bleModel];
}

// 发送数据
- (IBAction)sendData:(UIButton *)sender {
    
    if ([self.sendTf.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"发送内容不能为空"];
        return;
    }
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"透传：APP发送到串口端；串口发送会回传给APP如下显示" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        MLBleParamsItem *item = [MLBleParamsItem new];
        item.bleModel = self.bleModel;
        item.lotDict = @{
            @"type": self.sendTf.text
          };

        [[MLBLEManager sharedManager] sendUartData:item resultBlock:^(MLBleResultInfos * _Nonnull result) {
            self.resultLb.text = result.resultStr;
        }];
        
    }];

    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];

    
    
   
    
}

// 查询模组信息
- (IBAction)search:(UIButton *)sender {
    
    MLBleParamsItem *item = [MLBleParamsItem new];
    item.bleModel = self.bleModel;
    item.lotDict = @{
        @"type": @"Query"
      };
    [[MLBLEManager sharedManager] getDeviceInfo:item resultBlock:^(MLBleResultInfos * _Nonnull result) {
        self.resultLb.text = result.resultStr;
    }];
}

// ota
- (IBAction)Ota:(id)sender {
    
    MLBleParamsItem *item = [MLBleParamsItem new];
    item.bleModel = self.bleModel;
    item.lotDict = @{
        @"type":@"OtaReply",
        @"data": @{
            @"OTP":@(0),
            @"DId":@"",
            @"Token":@"53D161924A09210C8735867705E01E50",
            @"OtaUrl":@"https://yair.51miaomiao.cn/miotlink/thirdFactory/upgrade"
        }
    };
    
    [[MLBLEManager sharedManager] startOtaOnline:item resultBlock:^(MLBleResultInfos * _Nonnull result) {
        self.resultLb.text = result.resultStr;
    }];
}

- (void)showResultLb:(NSString *)text {
    self.resultLb.text  = text;
}

@end
